function W = bciMultiCompareFeatures(bci,Train,method,resh)
% bciMultiCompareFeatures(bci,Train,method)
% compares features in one-vs-one scheme, if more than 2 conditions
% and reshapes the data to its original size (good channels only)
% INPUT:
%   bci - bci parameter struct
%   Train - Train data created with bciGetTrainDat
%   method - string of comparison method:
%             'tval' - t-values
%             'rsqu' - Rsquare values
%             'svm' - weights of a support vector machine
%   resh (optional) - reshape the weight/contrast vector
%             0 - no reshaping
%             1 - reshape to good channel space
%             2 - reshape to all channel space (default)
% OUTPUT: 
%   cell array W with one calculation per condition pair
%

if nargin <4,
    resh = 2;
end

cond=bci.eventsToClassify;
nPairs = (length(cond)-1)*length(cond)/2;

W=cell(1,nPairs);

% one vs one
k=0;
for k1=1:length(cond)-1,
    for k2=k1+1:length(cond),
        k=k+1;
        idx = Train.label==cond(k1) | Train.label==cond(k2);
        Wtmp=zeros(1,size(bci.featureMask,2));
        Wtmp(bci.featureMask) = bciCompareFeatures(Train.dat(idx,:),Train.label(idx), method,bci.param.movdirC,[cond(k1),cond(k2)]);        
        switch resh,
            case 0,
                W{k}=Wtmp;
            case 1,
                W{k} = reshape(Wtmp,bci.reshape.reSize);
            case 2,
                WtmpReshaped = reshape(Wtmp,bci.reshape.reSize);
                [chIdx, defNChan]=bciChanMapping(bci,bci.goodChan,'ep','act');
                W{k} = zeros(defNChan, size(WtmpReshaped,2));
                W{k}(chIdx,:) = WtmpReshaped;
        end
    end
end